package com.example.difforder;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AllPotentialAnagrams {

    Random ran = null;
    /**
     * Write a program to generate all potential anagrams of an input string.
     * <p>
     * For example, the potential anagrams of "biro" are
     * <p>
     * biro bior brio broi boir bori
     * ibro ibor irbo irob iobr iorb
     * rbio rboi ribo riob roib robi
     * obir obri oibr oirb orbi orib
     *
     * @param in
     */
    public  void getAllPotentialAnagrams(String in) {
        String[] split = in.split("");
        List<String> strings = Arrays.asList(split);
        List<String> combination = getCombination(strings);
        Stream<String> sorted = combination.stream().sorted();
        sorted.forEach(System.out::println);

    }

    private List<String> getCombination(List<String> oneCharList) {
        List<String> result = new ArrayList<>();
        int allNum = getAllNum(oneCharList.size());
        int count = 1;
        while (result.size() != allNum) {
            System.out.println("循环次数"+count);
            String randomWord = getRandomWord(oneCharList);
            if(!result.contains(randomWord)){
                result.add(randomWord);
            }
            count++;
        }

        return result;
    }

    private int getAllNum(int a) {

        int r = 1;
        for (int j = 1; j <= a; j++) {
            r = r * j;
        }
        System.out.println(r);
        return r;
    }

    private String getRandomWord(List<String> oneCharList) {
        StringBuilder sb = new StringBuilder();
        if(ran == null){
         ran  = new Random(oneCharList.size());
        }

        List<Integer> r = new Random().ints(0,oneCharList.size()).distinct().limit(oneCharList.size()).boxed().collect(Collectors.toList());

        for (int i = 0; i < r.size(); i++) {
            sb.append(oneCharList.get(r.get(i)));
        }
        return sb.toString();
    }
}
